import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-08-09
 * Time: 21:49
 */
public class MySQLUpdate {
    public static void main(String[] args) throws SQLException {
        //1.创建数据源 数据对象是啥,在哪
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java105?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("");
        //2.建立连接
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        //按实际需求输入
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入你要修改的学号:");
        int id  = scanner.nextInt();
        System.out.println("输入你要修改的名字:");
        String name = scanner.next();

        //3.构造sql语句 构造请求
        String sql = "update  student set name = ? where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setInt(2,id);
        System.out.println("statement:" + statement);

        //4.执行sql语句  发送请求&读取响应
        int n = statement.executeUpdate();
        System.out.println(n);

        //5.释放先关资源
        statement.close();
        connection.close();

    }
}
